/* e_school_chars.do - *********************************************************

	Builds school level characteristics for a school

******************************************************************************/
local school_char_behav = 1
set more off
******************** School Level Characteristics ******************************

foreach t in oct eoy {
	local fyr 2001

	local lyr 2017

	*Eoy indicates the end of the school year so we need to push the year
	*forward one
	if "`t'"=="eoy" {
		local fyr 2002

		local lyr 2018
	}

	forvalues y = `fyr'/`lyr' {
		local yyo = substr("`y'",-2,2)
		use "$raw_data_sims/sims`t'`yyo'.dta", clear


		*clean variable names
		cap rename lowinc low_inc
		cap rename ORG_CODE org_code
		cap rename truant truancy
		cap rename IN_SUSP in_susp
		cap rename OUT_SUSP out_susp
		cap rename CITY_OB city_ob
		cap rename LVL_NEED lvl_need
		cap rename ELP_PROG elp_prog
		cap rename elp_eng_lrners_pgm_code elp_prog
		cap gen low_inc = ""
		cap gen rfr = ""		// reason for reporting
		cap gen enstat = ""		// enrollment status
		cap gen rfe = "" 		// reason for enrollment
		cap gen member = .a		// number of days a member of school
		cap gen attend = .a		// number of days attended
		cap gen plan = ""		// High school completer plan
		cap gen in_susp = .
		cap gen out_susp = .
		cap gen truancy = .
		cap gen spedeval = ""
		destring member, replace force
		destring attend, replace force
		destring in_susp, replace force
		destring truancy, replace force
		tostring low_inc, replace force
		tostring enstat, replace force
		tostring rfe, replace force
		tostring plan, replace force
		tostring spedeval, replace force
		destring enstat, replace
		cap rename town_re town
		cap gen spd_placement = ""



		*Fix up Special education results

		cap rename spd_sped_prmy_disb_code 	spd_prim_dis
		cap rename sns_sped_natr_svcs_code 	spd_natr_svcs
		cap rename sln_sped_lvl_need_code 	spd_lvl_need
		cap rename lvl_need					spd_lvl_need
		cap rename sped 					spd_placement

		cap gen spd_lvl_need = ""

		cap gen elp_prog = "miss"
		gen ell_pgm = elp_prog != "00" if elp_prog != "miss"

		keep in_susp out_susp truancy school grade sasid

		gen len_school = length(school)
		replace school = substr(school, -4, len_school)

		replace truancy = . if truancy < 0 | truancy > 365
		replace in_susp = . if in_susp < 0 | in_susp > 365
		replace out_susp = . if out_susp < 0 | out_susp > 365
		collapse (sum) in_susp out_susp truancy, by(sasid school grade)
		destring sasid, gen(sasid_no)
		collapse in_susp out_susp truancy (count) num_student = sasid_no, by(school grade)

		compress
		save "$stata_data_sims/`y'/school_level_`t'.dta", replace

	}

}

clear
foreach t in oct eoy {
	local fyr 2001
	local lyr 2017
	if "`t'"=="eoy" {
		local fyr 2002
		local lyr 2018
	}

	forvalues y = `fyr'/`lyr' {
		append using "$stata_data_sims/`y'/school_level_`t'.dta"
		cap gen simyear = `y'
		replace simyear = `y' if missing(simyear)
		cap gen t = "`t'"
		replace t = "`t'" if missing(t)

	}
}
replace in_susp = 0 if missing(in_susp)
replace out_susp = 0  if missing(out_susp)

gen total_susp = in_susp+ out_susp
collapse (sum) total_susp truancy num_student [aweight = num_student], by(simyear grade school)
collapse (mean) total_susp truancy [aweight = num_student], by(grade school)
keep if inlist(grade, "04","08","10")
destring grade, replace
compress
rename total_susp sch_total_susp
rename truancy sch_truancy

save "$stata_data_sims/school_level.dta", replace
